import { useEffect, useState } from 'react';
import { Modal, Button, message } from 'antd';
import { useTranslation } from 'react-i18next';
import { ec2Hold } from '@/service';
import styles from './index.module.scss';

const ModalLogin = (props) => {
    const { t } = useTranslation();
    const { visible, onClose, selectedRowKeys } = props;
    const [messageApi, contextHolder] = message.useMessage();
    const [btnLoading, setBtnLoading] = useState(false);
    const handleClose = () => {
        onClose();
    };
    const confirmFn = () => {
        let params = {
            amiDbId: selectedRowKeys
        }
        setBtnLoading(prevState => ({ ...prevState, btnLoading: true }));
        ec2Hold(params).then(res => {
            if (res.code == 0) {
                messageApi.open({
                    type: 'success',
                    content: res?.message,
                });
                handleClose();
                setBtnLoading(prevState => ({ ...prevState, btnLoading: false }));
            } else {
                messageApi.open({
                    type: 'error',
                    content: res?.message,
                });
                setBtnLoading(prevState => ({ ...prevState, btnLoading: false }));
            }
        }).catch(e => {
            messageApi.open({
                type: 'error',
                content: e?.message,
            });
            setBtnLoading(prevState => ({ ...prevState, btnLoading: false }));
        });

    }
    return (
        <Modal
            className={styles['container']}
            open={visible}
            // onCancel={handleClose}
            width={374}
            closable={false}
            destroyOnClose={true}
            maskClosable={false}
            title={t('common.check.title')}
            footer={null}
        >
            <div className={styles['header']}>{t('common.check.image.countent')}</div>
            <div className={styles['footer']}>
                <Button onClick={handleClose}>{t('common.check.goInstance')}</Button>
                <Button type="primary" onClick={confirmFn} loading={btnLoading}>{t('common.modal.button.submit')}</Button>
            </div>
            {contextHolder}
        </Modal>

    )
}

export default ModalLogin;
